package com.itump.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * <p>标题: JAVA标准化管理子系统</p>
 * <p>作者：itump</p>
 * <p>功能描述: 格式转换</p>
 * <p>创建时间：2011-07-15 19：25 </p>
 * <p>版本信息：1.0 </p>
 * <p>Copyright: Copyright (c) 2011</p>
 * <p>Company:</p>
 */
public class Formater {
	private static Log log = LogFactory.getLog(Formater.class);

	private static SimpleDateFormat monthFormat = new SimpleDateFormat(
			"yyyy-MM");

	private static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");

	private static SimpleDateFormat timeFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	
	private static SimpleDateFormat ymFormat = new SimpleDateFormat("yyyyMM");

	/**
	 * 功能: 字符串转换为Date类
	 * 
	 * @param strDate
	 *            指定日期
	 * @return 指定年月日的Date类，格式为yyyy-mm-dd
	 */
	public static Date strToDate(String strDate) {
		if (strDate == null || strDate.trim().equals(""))
			return null;

		try {
			Date date = dateFormat.parse(strDate);
			return date;
		} catch (Exception e) {
			log.error("Parse To Date Error:" + strDate);
			return null;
		}
	}
	/**
	 * 功能:加一年
	 * 
	 * @param strDate指定日期 加年数 日期格式:"yyyy"
	 *            
	 * @return STRING
	 */
	public static String dateAddNextYear(String strDate,int iNext) {
		if (strDate == null || strDate.trim().equals(""))
			return null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
		try {
			Calendar c1 = Calendar.getInstance();
			Date date = formatter.parse(strDate);
	        c1.setTime(date); 	  
	        int year = c1.get(Calendar.YEAR);
			c1.set(Calendar.YEAR, year+iNext);				
			return formatter.format(c1.getTime());
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 功能:加一月
	 * 
	 * @param strDate指定日期 加月数 日期格式:"yyyy-MM"
	 *            
	 * @return STRING
	 */
	public static String dateAddNextMonth(String strDate,int iNext) {
		if (strDate == null || strDate.trim().equals(""))
			return null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
		try {
			Calendar c1 = Calendar.getInstance();
			Date date = formatter.parse(strDate);
	        c1.setTime(date); 	  
	        int month = c1.get(Calendar.MONTH);
	        System.out.println("month="+month);
			c1.set(Calendar.MONTH, month+iNext);				
			return formatter.format(c1.getTime());
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 功能: 日期字符串转换成指定格式的日期字符串
	 * 
	 * @param strDate
	 *            原字符串
	 * @return 转换完成的字符串，格式为yyyy-mm-dd
	 */
	public static String strToStr(String strDate) {
		String date = Utils.chgNull(strDate);
		if (date.equals("")) {
			return "";
		}
		try {
			String retStr = dateFormat.format(dateFormat.parse(date));
			return retStr;
		} catch (Exception e) {
			log.error("Format To String Error:" + strDate);
			return "";
		}
	}

	/**
	 * 功能: 取出当前时间，并转化成字符串
	 * 
	 * @return String
	 */
	public static String dateToStr() {
		return dateFormat.format(new java.util.Date());
	}
	
	/**
	 * 功能: 取出当前时间，并转化成字符串
	 * @param y type of String
	 * @param m type of String
	 * @return String
	 */
	public static String dateToStr(String y,String m) {
		String ym = ymFormat.format(new java.util.Date());
		
		if (y != null && y.length() > 0) {
			ym = y + ym.substring(4);
		}
		if (m != null && m.length() > 0) {
			ym = ym.substring(0,4) + m;
		} else {
			ym = ym.substring(0,4);
		}
		
		return ym;
	}

	/**
	 * 功能: Date类转换成字符串
	 * 
	 * @param date
	 *            指定的日期
	 * @return 转换完成的字符串，格式为yyyy-mm-dd
	 */
	public static String dateToStr(Date date) {
		try {
			if (date != null) {
				String strDate = dateFormat.format(date);
				return strDate;
			} else
				return "";

		} catch (Exception e) {
			log.error("Format To String Error:" + date);
			return "";
		}
	}

	/**
	 * 功能: 提取Date中的时间
	 * 
	 * @param date
	 *            指定日期
	 * @return 指定日期中的时间，格式为HH:mm:ss
	 */
	public static String timeToStr(Date date) {
		try {
			String strDate = timeFormat.format(date);
			return strDate;
		} catch (Exception e) {
			log.error("Format To String Error:" + date);
			return "";
		}
	}

	/**
	 * 功能: 字符串转换为Date类
	 * 
	 * @param strDate
	 *            指定日期
	 * @return 指定年月日的Date类，格式为yyyy-mm-dd HH:mm:ss
	 */
	public static Date strToTime(String strDate) {
		try {
			Date date = timeFormat.parse(strDate);
			return date;
		} catch (Exception e) {
			log.error("Parse To Date Error:" + strDate);
			return null;
		}
	}

	/**
	 * 功能: 返回一个sql.date类型的时间
	 * 
	 * @param year
	 *            type of int
	 * @return java.sql.Date
	 */
	public static java.sql.Date getSqlDate(int year) {
		java.sql.Date d = null;
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, c.get(Calendar.YEAR) + year);
		long time = c.getTimeInMillis();
		d = new java.sql.Date(time);

		return d;
	}

	/**
	 * 功能: 返回一个sql.date类型的时间
	 * 
	 * @param year
	 *            type of int
	 * @return java.sql.Date
	 */
	public static String getInitYear(int year, int month) {
		Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH);
		if (m < month)
			y = y + year;

		return String.valueOf(y);
	}

	/**
	 * 功能: 获取当前月份
	 * 
	 * @return
	 */
	public static String CurrMonth() {
		Calendar c = Calendar.getInstance();
		return String.valueOf(c.get(Calendar.YEAR))
				+ String.valueOf(c.get(Calendar.MONTH));
	}

	/**
	 * 功能: 当前月的前几个月 200502的前2个月为200412
	 * 
	 * @param iPrev
	 * @return
	 */
	public static String PrevMonth(int iPrev) {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH);
		c.set(Calendar.MONTH, month - iPrev);
		String tempmonth="";
		if(c.get(Calendar.MONTH)<9)
			 tempmonth = "0"+String.valueOf(c.get(Calendar.MONTH)+1);	
		else
		     tempmonth = String.valueOf(c.get(Calendar.MONTH)+1);
		return String.valueOf(c.get(Calendar.YEAR))+ tempmonth;
	}

	/**
	 * 功能: 当前月的后几个月 2005-02的后2个月为2005-04
	 * 
	 * @param iPrev
	 * @return
	 */
	public static String NextMonth(int iNext) {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH) + 1;
		c.set(Calendar.MONTH, month + iNext);
		String strMonth = String.valueOf(c.get(Calendar.YEAR)) + "-"
				+ String.valueOf(c.get(Calendar.MONTH));
		try {
			Date date = monthFormat.parse(strMonth);
			String str =monthFormat.format(date); 
			return str;
		} catch (ParseException e) {
			log.error("Parse To Date Error:" + strMonth);
			return null;
		}
	}

	/**
	 * 功能: 今天的后几天
	 * 
	 * @param iNext
	 * @return
	 */
	public static String NextDay(int iNext) {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		now.add(Calendar.DAY_OF_YEAR, iNext);
		return formatter.format(now.getTime());
	}

	/**
	 * 功能: 明年
	 * 
	 * @return
	 */
	public static String NextYear() {
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		c.set(Calendar.YEAR, year + 1);
		return String.valueOf(c.get(Calendar.YEAR));
	}
	
	/**
	 * 功能: 当年
	 * 
	 * @return
	 */
	public static String CurrYear() {
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);		
		return ""+year;
	}
		

	/**
	 * 功能: 今天的前几天
	 * 
	 * @param iNext
	 * @return
	 */
	public static String PreDay(int iNext) {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		now.add(Calendar.DAY_OF_YEAR, -iNext);
		return formatter.format(now.getTime());
	}

	/**
	 * 功能: 取得指定月份的最后一天
	 * 
	 * @param strDate
	 * @return
	 */
	public static String getLastDate(String strDate) {
		return getLastDate(strToDate(strDate));
	}
	
	/**
	 * 功能: 取得指定月份的最后一天
	 * 
	 * @param strDate
	 * @return
	 */
	public static String getLastDate(Date date) {
		Calendar cstemp = Calendar.getInstance();
		cstemp.setTime(date);
		cstemp.add(Calendar.MONTH, 1);
		cstemp.add(Calendar.DATE, -1);
		return dateFormat.format(cstemp.getTime());
	}
	/**
	 * 功能: 获取当前月份1号
	 * 
	 * @return
	 */
	public static String getTopday() {
		Calendar c = Calendar.getInstance();
		String tempdate = String.valueOf(c.get(Calendar.YEAR));
		String tempmonth = "";
		if(c.get(Calendar.MONTH)<9)
			 tempmonth = "0"+String.valueOf(c.get(Calendar.MONTH)+1);	
		else
		     tempmonth = String.valueOf(c.get(Calendar.MONTH)+1);
		return tempdate+"-"+ tempmonth+"-"+"01";
	}
	/**
	 * 功能: 获取当前月份最后一号
	 * 
	 * @return
	 */
	public static String getLastday() {
		Calendar c = Calendar.getInstance();
		String tempdate = String.valueOf(c.get(Calendar.YEAR));
		String tempday = "";
		String tempmonth = "";
		String varmonth = String.valueOf(c.get(Calendar.MONTH)+1);
		if("3578".indexOf(varmonth)>=0) tempday = "31";
		if("469".indexOf(varmonth)>=0) tempday = "30";
		if("1".equals(varmonth) || "10".equals(varmonth) || "12".equals(varmonth)) tempday = "31";
		if("11".equals(varmonth)) tempday = "30";
		if("2".equals(varmonth)) {
			if(c.get(Calendar.YEAR)%4==0){
			     tempday = "28";
			}else{
				 tempday = "29";
			}
		}
		if(c.get(Calendar.MONTH)<9)
			 tempmonth = "0"+String.valueOf(c.get(Calendar.MONTH)+1);	
		else
		     tempmonth = String.valueOf(c.get(Calendar.MONTH)+1);			
		return tempdate+"-"+tempmonth+"-"+tempday;
	}
	
	/**
	 * 功能: 返回指定日期的后几个月的日期；如2006-1-1后13个月为200702
	 * 杨勇 2007.05.23
	 * @param datetime
	 * @param iPrev
	 * @return 200702
	 */
	public static Date getDate(String datetime,int iNext)throws Exception {
		java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd");
		java.util.Date sbegindate = dateFormat.parse(datetime);      //转换为日期
		Calendar c = Calendar.getInstance();
		c.setTime(sbegindate);
		int month = c.get(Calendar.MONTH);
		c.set(Calendar.MONTH, month + iNext);		
		try {
			Date date = c.getTime();
			return date;
		} catch (Exception e) {
			log.error("Parse To Date Error:" );
			return null;
		}
	}
	/**
	 * 功能: 得到下周一的日期
	 * yangyong 2007.11.28
	 * @param 
	 * @return
	 */
	public static String nextWeekDay() {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		int curday = now.get(Calendar.DAY_OF_WEEK);
		now.add(Calendar.DAY_OF_YEAR, 9-curday);
		return formatter.format(now.getTime());
	}
	/**
	 * 功能: 得到下周五的日期
	 * yangyong 2007.11.28
	 * @param 
	 * @return
	 */
	public static String lastWeekDay() {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		int curday = now.get(Calendar.DAY_OF_WEEK);
		now.add(Calendar.DAY_OF_YEAR, 9-curday+4);
		return formatter.format(now.getTime());
	}
}
